package com.gift.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.gift.common.JsonMapper;
import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.dao.GameAccountMapper;
import com.gift.pojo.GameAccount;
import com.gift.service.GameAccountService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("gameAccount")
public class GameAccountController {

    @Resource
    GameAccountService gameAccountService;
    @Resource
    GameAccountMapper gameAccountMapper;

    //跳转到游戏账号管理首页
    @RequestMapping("/home")
    public String goHome(){
        return "GameAccountController/gameAccount";
    }

    //跳转到新增游戏账号页面
    @RequestMapping("/newGameAccount")
    public String newGame(){
        return "GameAccountController/newGameAccount";
    }

    //跳转到修改游戏账号页面
    @RequestMapping(value = "/alterGameAccount",method = RequestMethod.GET)
    public String alterGame(@Param("aid")int id, Model model){
        GameAccount gameAccount=gameAccountMapper.selectByPrimaryKey(id);
        model.addAttribute("gameAccount",gameAccount);
        return "GameAccountController/alterGameAccount";
    }

    //得到所有游戏账号(分页)
    @RequestMapping("/getAll")
    @ResponseBody
    public PaGing getAll(int page, int rows){

        return gameAccountService.getAllBypr(page,rows);
    }

    //得到所有游戏账号（不分页）
    @RequestMapping("/getAllNo")
    @ResponseBody
    public List getAllNo(){

        return gameAccountService.getAll();
    }

    //保存新增游戏账号
    @RequestMapping("/insetNewGameAccount")
    @ResponseBody
    public ServerResponse insetNewGameAccount(@Param("datalist") String datalist){

        GameAccount newOneGamesAccount = JsonMapper.string2Obj(datalist, new TypeReference<GameAccount>() {
        });

        return gameAccountService.insetNewGameAccount(newOneGamesAccount);
    }

    //修改游戏账号信息
    @RequestMapping("/updataGameAccount")
    @ResponseBody
    public ServerResponse updataGame(@Param("datalist") String datalist){

        GameAccount alterGames = JsonMapper.string2Obj(datalist, new TypeReference<GameAccount>() {
        });

        return gameAccountService.updataGameAccount(alterGames);
    }

    //删除游戏账号信息
    @RequestMapping(value = "deleteAll", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteAll(@Param("deleteIdString") String deleteIdString ) {

        String[] strings= StringUtils.split(deleteIdString,",");
        ArrayList<Integer> deleteIdList = new ArrayList<>();
        for(int i = 0;i<strings.length;i++){
            deleteIdList.add(Integer.valueOf(strings[i]));
        }
        return  gameAccountService.deleteGidList(deleteIdList);
    }

    //根据游戏得到对应的游戏账号列表
    @RequestMapping("/getAllByGid")
    @ResponseBody
    public List getAllByGid(@RequestParam("gid") String gid){

        return gameAccountMapper.getAllByGid(Integer.valueOf(gid));
    }

}
